<template>
<mt-loadmore :top-method="loadTop" :bottom-method="loadBottom" :bottom-all-loaded="allLoaded" ref="loadmore">
        <div class="goods-list">
            <div class="goods-item" v-for="(item,index) in goodsList" :key="index">
            <img :src="item.img_url" alt="">
            <h1>{{item.title}} </h1>
            <div class="info">
            <p class="price">
                <span class="now">￥{{item.market_price}}</span>
                <span class="old">￥{{item.sell_price}}</span>
            </p>
            <p class="sell">
                <span>热卖中</span>
                <span>剩{{item.stock_quantity}}件</span>
            </p>
            </div>
            </div>
        </div>
</mt-loadmore>
   
</template>
<script>
import { goodsList } from "@/api";
export default {
  data() {
    return {
      goodsList: [],
      pageindex: 1,
      allLoaded: false
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      goodsList(this.pageindex).then(res => {
        console.log(res);
        if(this.pageindex == 1){
        //重置下拉状态  刷新页面
          this.$refs.loadmore.onTopLoaded();
            this.goodsList = res.message;
        }else{
          //如果数据加载完毕，上拉加载事件就不在执行
          if(res.message.length == 0){
            //上拉加载数据的事件为false就执行  true 不执行
            this.allLoaded = true;
            alert('没有更多数据了')
          }
          //重置下拉状态，刷新页面
          this.$refs.loadmore.onTopLoaded();
          // 加载更多的数据
          this.goodsList = this.goodsList.concat(res.message)

        }
      });
    },
    loadTop() {
      // load more data
      this.pageindex=1
      this.$refs.loadmore.onTopLoaded();
      //为false 时 下拉更新更多数据
      this.allLoaded = false;
      this.getData()
    },
    loadBottom() {
      // load more data
      this.pageindex++;
      this.allLoaded = false; // if all data are loaded
      this.$refs.loadmore.onBottomLoaded();
      this.getData()
    }
  }
};
</script>
<style lang="less" scoped>
.goods-list {
  display: flex;
  padding: 7px;
  justify-content: space-between;
  //   background-color: pink;
  flex-wrap: wrap;
  .goods-item {
    width: 49%;
    // background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 4px 0;
    padding: 2px;
    display: flex;
    flex-direction: column;
    min-height: 293px;
    justify-content: space-between;
    img {
      width: 100%;
    }
    h1 {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      //   padding: 5px;
      .price {
        margin: 0;
        padding: 5px;
        .now {
          color: red;
          font-size: 16px;
          font-weight: 700;
        }
        .old {
          text-decoration: line-through;
          margin-left: 10px;
          font-size: 12px;
        }
      }
      .sell {
        font-size: 13px;
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>


